import { Consts } from "../Data/Consts";
import { bdlMgr } from "../Manager/BundleManager";
import { randomRangeInt } from "../utils/ccUtils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TouchPanel extends cc.Component {
    public static Instance: TouchPanel;

    @property(cc.Node)
    touchCircle: cc.Node = null;

    onLoad() {
        TouchPanel.Instance = this;
    }

    start() {
        // this.node.on(cc.Node.EventType.TOUCH_START, (t) => {
        //     const location = t.getLocation();
        //     const currentPos = this.node.parent.convertToNodeSpaceAR(location);
        //     this.ShowTouchCircle(currentPos);
        // }, this, true);

        this.node.on(cc.Node.EventType.TOUCH_START, (t) => {
            cc.systemEvent.emit(Consts.EventType.TOUCH_SCREEN);

            const location = t.getLocation();
            const currentPos = this.node.parent.convertToNodeSpaceAR(location);
            this.ShowTouchCircle(currentPos);
        }, this, true);

    }

    async ShowTouchCircle(_pos: cc.Vec2 = cc.v2(0, 0)) {
        return;

        const node: cc.Node = cc.instantiate(this.touchCircle);
        node.active = true;
        node.parent = this.node;

        let ParticleSystem_explode = node.getComponent(cc.ParticleSystem)
        const color: number = randomRangeInt(0, 4);
        const sp = await bdlMgr.getRes<cc.SpriteFrame>(Consts.BundleName.SPRITE, `shitou/shitou_00${color + 1}`, cc.SpriteFrame);
        const ps = ParticleSystem_explode.getComponent(cc.ParticleSystem);
        ps.spriteFrame = sp;

        node.setPosition(0, 0);
        node.setPosition(_pos);

        node.opacity = 255;
        node.scale = 1;
        node.width = 10;
        node.height = 10;
        // const duration = 0.5;
        cc.tween(node)
            .to(0.4, { width: 100, height: 100 }, { easing: '' })
            .to(0.4, { opacity: 0 }, { easing: '' })
            .call(() => {
                node.destroy();
            })
            .start()

        // TipPanel.Instance.Show("解锁成功！！！");
    }

}
